import json

import requests
import re
from util.headers import get_header
from util.logger import get_looger
import os, shutil

page = 1
loger = get_looger("番剧.log", __file__)
loger.info("爬虫开始")

root = "static/json_data/"
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)


try:
    while True:

        loger.info(f"正在爬取第{page}页")
        url = f"https://api.bilibili.com/pgc/season/index/result?st=1&order=3&season_version=-1&spoken_language_type=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page={page}&season_type=1&pagesize=20&type=1"
        res = requests.get(url, headers=get_header())
        if res.status_code == 200:
            json_data = res.json()
            datas = json_data["data"]
            items = datas["list"]
            for item in items:
                with open(f"{root}{page}.json", "w") as f:
                    json_item = json.dumps(item)
                    f.write(json_item)
            if datas["has_next"]:
                break
            else:
                page += 1
        else:
            loger.error(res.status_code)

except Exception as e:
    loger.error(e)

loger.info("爬取完毕")
